import { defineStore } from 'pinia';
import { OrderRow } from './radar-chart/model';

export const useTerminalStore = defineStore('terminal-crane1', {
  state: () => ({
    orderData: [] as OrderRow[],
    orderType: '',
    orderTypeOptions: ['卸货', '发货', '上料', '收料', '倒垛', '退料'],
    highlighted: '', // 定义 highlighted
    number: 0, // 定义 number
  }),
  getters: {
    currentOrderNo(state) {
      const item = state.orderData.find(v => v.orderStatus !== '2');
      return item;
    },
    filterOrderData(state) {
      const item = state.orderData.find(v => v.orderStatus !== '2');
      const orderNo = item?.orderNo;

      //先判断是否存在orderStatus !== 2的指令,存在--只显示本指令的标签，不存在根据--6米内显示标签，6米外不显示
      //再根据orderType筛选
      if (orderNo) {
        if (state.orderType) {
          return state.orderData
            .filter(v => v.orderType === state.orderType)
            .map(v1 => {
              return {
                ...v1,
                isShow_label: v1.orderNo === orderNo,
              };
            });
        } else {
          return state.orderData.map(v1 => {
            return {
              ...v1,
              isShow_label: v1.orderNo === orderNo,
            };
          });
        }
      } else {
        //6米内显示标签，6米外不显示
        if (state.orderType) {
          return state.orderData
            .filter(v => v.orderType === state.orderType)
            .map(v1 => {
              const isShowLabel = Math.abs(v1.frX) < 150000;
              console.log(`frX: ${v1.frX}, isShowLabel: ${isShowLabel}`);
              return {
                ...v1,
                isShow_label: Math.abs(v1.frX) < 150000,
              };
            });
        } else {
          return state.orderData.map(v1 => {
            return {
              ...v1,
              isShow_label: Math.abs(v1.frX) < 150000,
            };
          });
        }
      }
    },

    //判断行车是否到位
    isArrive(state) {
      const item = state.orderData.find(v => v.orderStatus !== '2');
      if (item) {
        if (item.orderStatus === '13') {
          if (Math.abs(item.toX) <= 1000 && Math.abs(item.toY) <= 1000) {
            return true;
          } else {
            return false;
          }
        } else {
          if (Math.abs(item.frX) <= 1000 && Math.abs(item.frY) <= 1000) {
            return true;
          } else {
            return false;
          }
        }
      } else {
        return false;
      }
    },
  },
  actions: {},
});
